package com.coffee.wx.uitls;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ConstellationUtils {

    /**
     * 获取星座运势
     * @param consName
     * @return
     */
    public static String getConstellation(String consName) {

        // 发送http请求的url
        String url = "http://web.juhe.cn/constellation/getAll";

        Map<String, String> params = new HashMap<String, String>();
        params.put("key", "3ea98065f899a5ce23e7172c4bfbdbd1"); // 在个人中心->我的数据,接口名称上方查看
        params.put("consName", consName); // 星座名称，如:狮子座
        params.put("type", "today"); // 运势类型：today,tomorrow,week,month,year

        String paramsStr = TypeChangeUtils.urlencode(params);

        String response = SendHttpUtils.doGet(url, paramsStr);
        // post请求
        // String response = doPost(url,paramsStr);

        return buildResultDate(response);
    }


    private static String buildResultDate(String response) {
        String result = "";
        Map bean = JSONUtil.toBean(response, Map.class);
        if (MapUtil.isNotEmpty(bean)) {
            Iterator iterator = bean.keySet().iterator();
            while (iterator.hasNext()){
                Object key = iterator.next();
                result += key + "：" + bean.get(key) + "\n";
            }
        }
        return result;
    }

}
